﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MonitorCode;
using MonitorCode.Hangfire;
using YouShuCode;

namespace DevSupport.Areas.Monitor
{
    [ApiController]
    public class HangfireController : ControllerBase
    {
        private ILogger<HangfireController> _logger;
        private readonly IHangfireService _jobSservice; 

        public HangfireController(ILogger<HangfireController> logger, IHangfireService jobSservice)
        {
            _logger = logger;
            _jobSservice = jobSservice;
        }
        /// <summary>
        /// 监控列表
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Monitor/JobList")]
        public object StartJob()
        {
            return new { total = JobTest.Jobs.Count, list = JobTest.Jobs }; 
        }
        /// <summary>
        /// 添加job
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Monitor/AddJob")]
        public bool AddJob(JobConfig dto)
        {
            var model = new JobConfig
            {
                JobId=dto.GroupName + dto.ServerName.Trim(),
                GroupName = dto.GroupName,
                ServerName= dto.ServerName.Trim(),
                ServerType  = dto.ServerType,
                CronExpression= dto.CronExpression,
                CheckPath= dto.CheckPath.Trim(),
                ServerUrl= dto.ServerUrl.Trim(),
                Status= JobStatus.No,
            };
            return _jobSservice.AddJobFire(model);
        }
        /// <summary>
        /// 开启监控
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Monitor/StartJob")]
        public bool StartJob(string JobId)
        {

            return _jobSservice.StartJobFire(JobId);
        }
        /// <summary>
        /// 停止监控
        /// </summary>
        /// <param name="JobId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Monitor/StopJob")]
        public bool StopJob(string JobId)
        {  
            return _jobSservice.DeleteJobFire(JobId);
        }

    }
}
